package com.sbtr.common.file.impl;

import com.alibaba.excel.EasyExcel;
import com.sbtr.common.file.utils.AbstractImportInterface;
import com.sbtr.common.file.utils.AbstractNoModelDataListener;
import com.sbtr.common.file.CommonAnalyzeService;
import com.sbtr.common.file.dto.AnalyzeDto;
import com.sbtr.common.file.dto.FileContentDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.*;

@Slf4j
@Component
public class CommonAnalyzeServiceImpl implements CommonAnalyzeService {

    @Override
    public List<LinkedHashMap<Integer, String>> analyze(Map<String, String> params, List<LinkedHashMap<Integer, String>> data) {
        return data;
        // 业务判断逻辑，此处伪代码，请自行实现
    }

    @Override
    public FileContentDto common(AnalyzeDto analyzeDto) {

        String path = analyzeDto.getPath();
        if(!path.endsWith(".txt")&&!path.endsWith(".csv")&&!path.endsWith(".xlsx")&&!path.endsWith(".xls")){
            throw new IllegalArgumentException("上传模块类型必须为txt,csv,xlsx,xls其中一个！！");
        }
        //设置数据存储
        FileContentDto fileContentDto = new FileContentDto();
        if (analyzeDto.getPath().toLowerCase().endsWith(".txt")) {
            if (analyzeDto.getTxtMethod()==null||analyzeDto.getTxtMethod()==0) {
                analyzeDto.setTxtMethod(2);
            }

            File file = new File(analyzeDto.getPath());
            FileInputStream fin = null;
            InputStreamReader reader = null;
            BufferedReader buffReader = null;
            try {
                fin = new FileInputStream(file);
                reader = new InputStreamReader(fin);
                buffReader = new BufferedReader(reader);
                String strTmp = "";

                List<String> title = new ArrayList<>();
                List<LinkedHashMap<Integer, String>> content = new ArrayList<>();
                for (int i = 0; (strTmp = buffReader.readLine()) != null; i++) {
                    String[] str = strTmp.trim().split("\\s+");
                    if (i == 0 &&analyzeDto.getTxtMethod()==1) {//横标题
                        title.addAll(Arrays.asList(str));
                        fileContentDto.setTitle(title);
                        continue;
                    }
                    LinkedHashMap<Integer, String> params = new LinkedHashMap<>();
                    for (int j = 0; j < str.length; j++) {
                        if (j == 0 &&analyzeDto.getTxtMethod()==2) {//竖标题
                            title.add(str[j]);
                            continue;
                        }
                        params.put(j, str[j]);
                    }
                    content.add(params);
                }

                fileContentDto.setTitle(title);
                fileContentDto.setContent(content);

                if(analyzeDto.getTxtMethod()==2){//转换成结构一致
                    fileContentDto.setContent(fileContentDto.lineGetListMap());
                }
            } catch (IOException e) {
                log.info("读取txt失败！", e.getMessage());
            } finally {
                try {
                    buffReader.close();
                    reader.close();
                    fin.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } else {//解析excel
            File file = new File(analyzeDto.getPath());
            AbstractNoModelDataListener abstractNoModelDataListener = new AbstractNoModelDataListener(this, new HashMap<>());
            EasyExcel.read(file, abstractNoModelDataListener).sheet().doRead();
            fileContentDto.setContent(abstractNoModelDataListener.resultDataList);
            fileContentDto.setTitle(abstractNoModelDataListener.titleList);

        }
        return fileContentDto;
    }


}
